<template>
  <div class="app-container">
    <el-row>
      <!-- 搜索区域 -->
      <el-card style="margin:0 0 5px 5px" v-show="showSearch">
        <el-form ref="queryForm" :inline="true" :model="queryData">
          <el-form-item label="任务名称" prop="Q_processName_S_LK">
            <el-input v-model="queryData.Q_processName_S_LK" placeholder="任务名称" />
          </el-form-item>
          <el-form-item label="流程分类" prop="Q_category_S_EQ">
            <el-select v-model="queryData.Q_category_S_EQ" placeholder="流程分类">
              <el-option label="全部" value="" />
              <el-option
                v-for="item in optionOfCategory"
                :key="item.dictCode"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item class="fr p-r-10">
            <el-button class="filter-item" type="primary" round icon="el-icon-search" @click="handleFilter">
              搜索
            </el-button>
            <el-button class="filter-item m-l-10" type="info" round icon="el-icon-refresh" @click="handleClear">
              清空
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- table区域 -->
      <el-card class="m-l-5">
        <el-row :gutter="10" class="m-b-5">
          <table-toolbar :showSearch.sync="showSearch" @refresh="getTableData" />
        </el-row>
        <el-table
          ref="task_table"
          v-loading="tableLoading"
          row-key="taskId"
          :data="tableData"
          element-loading-text="加载中"
          border
          fit
          highlight-current-row
          show-overflow-tooltip
          height="100px"
          v-el-height-adaptive-table="{bottomOffset: 110}"
        >
          <el-table-column align="center" label="任务编号" prop="taskId" width="150" show-overflow-tooltip />
          <el-table-column align="center" label="流程名称" prop="procDefName" width="200" show-overflow-tooltip />
          <el-table-column align="center" label="流程分类" width="150">
            <template slot-scope="scope">
              <el-tag :type="getDictType('category', scope.row.category)" v-text="getDictValue('category', scope.row.category)" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="任务名称" prop="taskName" />
          <el-table-column align="center" label="流程发起人" width="150">
            <template slot-scope="scope">
              <label>{{scope.row.startUserName}} <el-tag v-if="scope.row.startDeptName" type="info" size="mini">{{scope.row.startDeptName}}</el-tag></label>
            </template>
          </el-table-column>
          <el-table-column align="center" label="接收时间" prop="createTime" width="180" />
          <el-table-column align="center" label="审批时间" prop="finishTime" width="180" />
          <el-table-column align="center" label="任务耗时" prop="duration" width="150"/>
          <el-table-column align="center" label="办理人" width="150">
            <template slot-scope="scope">
              <label v-if="scope.row.assigneeName">{{scope.row.assigneeName}} <el-tag type="info">{{scope.row.deptName}}</el-tag></label>
              <label v-if="scope.row.candidate">{{scope.row.candidate}}</label>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="80" fixed="right">
            <template slot-scope="scope">
              <el-dropdown>
                <span class="el-dropdown-link">
                  选项<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item>
                    <el-link type="info" icon="el-icon-tickets" @click="handleDetail(scope.row)">流转记录</el-link>
                  </el-dropdown-item>
                  <el-dropdown-item v-permission="['flowable:task:delete']">
                    <el-link type="danger" icon="el-icon-delete" @click="handleDelete(scope.row.procInsId)">删除</el-link>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="tableQuery.total>0" :total="tableQuery.total" :page.sync="tableQuery.page" :limit.sync="tableQuery.limit" @pagination="getTableData" />
      </el-card>
    </el-row>

    <task-detail-dialog v-if="openTaskDetailDialog" ref="taskDetailDialog" :title="processName" @confirm="handleFilter" />
  </div>
</template>

<script>
import { categoryGroup, finishedTable, taskInstanceDelete } from '@/api/tool/workflow'
import TaskDetailDialog from './detail'

export default {
  components: {
    TaskDetailDialog
  },
  data() {
    return {
      showSearch: true,
      queryData: {
        Q_processName_S_LK: undefined,
        Q_category_S_EQ: undefined
      },
      tableLoading: false,
      tableData: [],
      tableQuery: {
        page: 1,
        limit: 20,
        total: 0,
        sort: undefined,
        order: undefined
      },
      selectionData: [],
      optionOfCategory: [],
      openTaskDetailDialog: false,
      processName: undefined
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      // 加载下拉菜单
      this.getDictData()
      this.getTableData()
    },
    getDictData() {
      categoryGroup().then(response => {
        this.optionOfCategory = response
      }).catch(e => {})
    },
    getDictType(property, value) {
      return this.$api.getDictElement(this.optionOfCategory, value)[0]
    },
    getDictValue(property, value) {
      return this.$api.getDictElement(this.optionOfCategory, value)[1]
    },
    handleFilter() {
      this.tableQuery.page = 1
      this.getTableData()
    },
    handleClear() {
      this.$refs.queryForm.resetFields()
      this.getTableData()
    },
    getTableData() {
      const query = Object.assign(this.tableQuery, this.queryData)
      this.tableLoading = true
      finishedTable(query).then(response => {
        this.tableData = response.param.data
        this.tableQuery.total = response.param.recordsTotal
        this.tableLoading = false
        this.$refs.task_table.bodyWrapper.scrollTop = 0
      }).catch(e => {
        this.tableLoading = false
      })
    },
    sortChange(data) {
      const { prop, order } = data
      this.tableQuery.sort = prop
      if (order === 'ascending') {
        this.tableQuery.order = 'asc'
      } else {
        this.tableQuery.order = 'desc'
      }
      this.getTableData()
    },
    handleDetail(row) {
      this.processName = row.procDefName
      this.openTaskDetailDialog = true
      this.$nextTick(() => {
        this.$refs.taskDetailDialog.init({
          procDefId: row.procDefId,
          procInsId: row.procInsId,
          deployId: row.deployId,
          taskId: row.taskId,
          finished: false
        })
      })
    },
    handleDelete(procInsId) {
      if (!procInsId || procInsId.length === 0) {
        this.$modal.msgWarning('未找到流程实例')
        return
      }
      this.$prompt('删除原因', '是否删除任务流程实例?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType:"textarea",
        inputValidator: (value) => {
          if (!value) {
            return '请输入删除原因'
          }
        }
      }).then(value => {
        taskInstanceDelete({
          instanceId: procInsId,
          reason: value
        }).then(response => {
          this.handleFilter()
          this.$modal.msgSuccess(response.msg)
        })
      }).catch(() => {})
    },
  }
}
</script>
